/*************************************************************************
	> File Name: EP35.cpp
	> Author: 
	> Mail: 
	> Created Time: 五  8/ 9 18:20:03 2019
 ************************************************************************/
#include <stdio.h>
#include <math.h>
#define max_n 1000000

int prime[max_n + 5] = {0};
int is_Prime[max_n + 5] = {0};

void init() {
    for (int i = 2; i <= max_n; i++) {
        if (!is_Prime[i]) prime[++prime[0]] = i;
        for (int j = 1; j <= prime[0]; j++) {
            if (prime[j] * i > max_n) break;
            is_Prime[prime[j] * i] = 1;
            if (i % prime[j] == 0) break;
        }
    }
    return ;
}

int is_val(int x) {
    int digit = floor(log10(x)) + 1;
    int tmp = x, h = (int)pow(10, digit - 1);
    for (int i = 0; i < digit; i++) {
        tmp = (tmp % 10) * h + tmp / 10;
        if (is_Prime[tmp]) return 0;
    }
    return 1;
}

int main() {
    init();
    int sum = 0;
    for (int i = 1; i <= prime[0]; i++) {
        if (!is_val(prime[i])) continue;
        sum += 1;
    }
    printf("%d\n", sum);
    return 0;
}
